package org.hotcommentssys.controller;

import org.hotcommentssys.entity.Comments;
import org.hotcommentssys.entity.Hotevents;
import org.hotcommentssys.service.CommentsService;
import org.hotcommentssys.service.HoteventsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Comments)表控制层
 *
 * @author makejava
 * @since 2021-12-04 08:43:38
 */
@Controller
@RequestMapping("comments")
public class CommentsController {
    /**
     * 服务对象
     */
    @Resource
    private CommentsService commentsService;

    //测试
    //111张智健

    @Resource
    private HoteventsService hoteventsService;

    @RequestMapping(value = "selectByHId")
    public String selectByHId(int hId, Model model) {
        Hotevents hotevents = hoteventsService.queryById(hId);
        Comments comments = new Comments();
        comments.setHoteventsid(hId);
        List<Comments> commentsList = commentsService.queryAll(comments);
        model.addAttribute("hotevents", hotevents);
        model.addAttribute("commentsList", commentsList);
        return "byIdList";
    }

    @RequestMapping(value = "insert")
    public String insert(int hoteventsid, String content) {
        Comments comments = new Comments();
        comments.setContent(content);
        comments.setHoteventsid(hoteventsid);
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String commentdate = format.format(date);
        comments.setCommentdate(commentdate);
        commentsService.insert(comments);
        return "redirect:/comments/selectByHId?hId=" + hoteventsid;
    }
}
